package com.maple.service.impl;

import com.maple.dto.ShoppingCartDTO;
import com.maple.entity.Dish;
import com.maple.entity.DishFlavor;
import com.maple.entity.Setmeal;
import com.maple.entity.ShoppingCart;
import com.maple.mapper.DishFlavorMapper;
import com.maple.mapper.DishMapper;
import com.maple.mapper.SetmealMapper;
import com.maple.mapper.ShoppingCartMapper;
import com.maple.service.ShoppingCartService;
import com.maple.utils.ThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    private final Integer  AMOUNT = 1;


    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
//      获得用户id
        Long userId = ThreadLocalUtils.getCurrentId();

//        根据用户id查询数据库
        List<ShoppingCart> list = shoppingCartMapper.queryByUserId(userId);

//        返回值
        return list;
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {
//        判断数据库中是否已经存在数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(ThreadLocalUtils.getCurrentId());
        ShoppingCart list = shoppingCartMapper.query(shoppingCart);
        if(list != null){
            BigDecimal oneAmount = list.getAmount().divide(BigDecimal.valueOf(list.getNumber())) ;
            list.setNumber(list.getNumber() + 1);
            list.setAmount(oneAmount.multiply(BigDecimal.valueOf(list.getNumber())));
            shoppingCartMapper.update(list);
            return;
        }

//        数据库中没有，则判断并封装对象
        shoppingCart.setCreateTime(LocalDateTime.now());
        if(shoppingCartDTO.getSetmealId() != null){
            Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setAmount(setmeal.getPrice());
        } else{
            Dish dish = Dish.builder()
                    .id(shoppingCart.getDishId())
                            .build();
            Dish query = dishMapper.query(dish);
            shoppingCart.setName(query.getName());
            shoppingCart.setImage(query.getImage());
            shoppingCart.setAmount(query.getPrice());
        }
        shoppingCart.setNumber(AMOUNT);

//        数据库添加
        shoppingCartMapper.add(shoppingCart);

    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void deleteInOne(ShoppingCartDTO shoppingCartDTO) {
        if(shoppingCartDTO == null){
        throw new RuntimeException();
    }
//        封装对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(ThreadLocalUtils.getCurrentId());

//        判断购物车中对应的数据量是否大于一
        ShoppingCart query = shoppingCartMapper.query(shoppingCart);
        if(query.getNumber() > 1){
            query.setNumber(query.getNumber() - 1);
            shoppingCartMapper.update(query);
            return;
        }

//        等于一，则数据库删除
        shoppingCartMapper.delete(shoppingCart);

    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {
        Long id = ThreadLocalUtils.getCurrentId();
        if(id == null){
            throw new RuntimeException();
        }
//        封装对象
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(id);

//        数据库删除
        shoppingCartMapper.delete(shoppingCart);
    }


}
